package server.synchronization;

import java.util.HashMap;

public class LockManager
{
	public enum LockOn { Insert, Update }
	private final HashMap<LockOn, LockList> locks;
	
	public LockManager()
	{
		locks = new HashMap<LockManager.LockOn, LockList>();
		
		for (LockOn lockOn : LockOn.values())
		{
			locks.put(lockOn, new LockList());
		}
	}
	
	public boolean isLocked(String username, LockOn lockOn)
	{
		return locks.get(lockOn).containsKey(username);
	}
	public void removeLock(String username, LockOn lockOn)
	{
		locks.get(lockOn).remove(username);
	}
	public void addLockedServerId(String username, LockOn lockOn, Integer serverId)
	{
		locks.get(lockOn).get(username).addLockedServerId(serverId);
	}
	public boolean fullLockEstablished(String username, LockOn lockOn, int connectedServerCount)
	{
		return locks.get(lockOn).get(username).canRelease(connectedServerCount);
	}
}
